package VacationHomework.LeetCode;

import java.util.Stack;

public class Num20 {
    public boolean isValid(String s) {
        char[] arr=s.toCharArray();
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            char tmp=arr[i];
            if(tmp=='(' || tmp=='[' || tmp=='{'){
                stack.push(tmp);
            }else {
                if(stack.isEmpty()){
                    return false;
                }
                char a=stack.peek();
                if(a=='(' && tmp!=')'){
                    return false;
                }
                if(a=='[' && tmp!=']'){
                    return false;
                }
                if(a=='{' && tmp!='}'){
                    return false;
                }
            }
        }
    return stack.isEmpty();
    }
}
